home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / usenet / sources / volume90 / util / rm / rm.asm < prev    next >
Assembly Source File  |  1990-01-28  |  7KB  |  388 lines

  1. ;- :ts=12 --------------------------------------------------------
  2. ;
  3. ;      rm -- remove files with Oops! possibility
  4. ;
  5. ;    /|||\          (c) Copyright 1989 by Martin J. Laubach
  6. ;   //|||\\            All rights reserved
  7. ;  ///|||\\\
  8. ;
  9. ;-----------------------------------------------------------------
  10.  
  11.     ifd CAPE
  12.     optimon
  13.     addsym
  14.     objfile "rm.o"
  15.     endc
  16.  
  17.     include "exec/types.i"
  18.     include "exec/alerts.i"
  19.     include "exec/libraries.i"
  20.     include "libraries/dos.i"
  21.     include "arp/arpbase.i"
  22.  
  23. call    macro
  24.     ifnd    _LVO\1
  25.     xref    _LVO\1
  26.     endc
  27.     jsr    _LVO\1(a6)
  28.     endm
  29.  
  30.     xref    _LVOAlert
  31.  
  32. ScratchSiz:    equ    256
  33. JunkSiz:    equ    80
  34.  
  35.     section    Remove,code
  36.  
  37. ;---------------------------------------------------------------------------
  38. ; Try to open arp.library first. Write NoArp text if failure
  39.  
  40. main:    RESIDENT    3000
  41.  
  42.     movem.l    a0/d0,-(a7)
  43.  
  44.     move.l    4,a6
  45.     lea    ArpN(pc),a1
  46.     moveq    #34,d0
  47.     call    OpenLibrary
  48.     tst.l    d0
  49.     bne.s    ArpOK
  50.     lea    DosN(pc),a1
  51.     moveq    #34,d0
  52.     call    OpenLibrary
  53.     tst.l    d0
  54.     bne.s    DosOK
  55.  
  56. ;-------------------------------------
  57. ; Recoverable alert if no dos.library found
  58.  
  59. alertDos:    ALERT    (AG_OpenLib!AO_DOSLib)
  60.  
  61. EFail:    moveq    #RETURN_FAIL,d0
  62.     rts
  63.  
  64. ;-------------------------------------
  65. ; Write OhAhNoArp text for user without arp.library
  66. ; Expects DOSBase in D0
  67.  
  68. DosOK:    move.l    d0,a6
  69.     call    Output
  70.     move.l    d0,d1
  71.     lea    NoArp(pc),a0
  72.     move.l    a0,d2
  73.     moveq    #NoArpEnd-NoArp,d3
  74.     call    Write
  75.  
  76.     move.l    a6,a1
  77.     move.l    4,a6
  78.     call    CloseLibrary
  79.  
  80.     bra.s    EFail
  81.  
  82. ;-------------------------------------
  83. ; Arp opened ok, now parse the command line
  84. ; Expects ArpBase in D0
  85.  
  86. ArpOK:    move.l    d0,a6
  87.     movem.l    (a7)+,a0/d0
  88.  
  89.     lea    CLIHelp(pc),a1
  90.     lea    -4(a7),a7
  91.     move.l    a7,a2
  92.     lea    CLITemp(pc),a3
  93.     call    GADS
  94.     move.l    (a7),a5
  95.     lea    4(a7),a7
  96.  
  97. ;-------------------------------------
  98. ; Check parameters, write usage if less than one
  99. ; Expects D0 number of paramaters
  100.  
  101.     tst.l    d0
  102.     bne.s    ParamOK
  103.     lea    Usage(pc),a1
  104.     call    Puts
  105.     bra.s    EFail
  106.  
  107. ;--------------------------------------------------------------------------
  108. ; Parameters ok, begin to work on them
  109. ; D0: argument count
  110. ; A5: argument vector
  111.  
  112. ParamOK:    move.l    d0,d7
  113.  
  114.     lea    -JunkSiz(a7),a7
  115.     move.l    a7,a2
  116.  
  117.     lea    Junk(pc),a0
  118.     move.l    a2,a1
  119.     moveq    #JunkSiz,d0
  120.     call    Getenv
  121.  
  122.     tst.b    (a2)
  123.     bne.s    NoEnv
  124.  
  125.     lea    Junk(pc),a2
  126.  
  127. NoEnv:    subq.l    #1,d7
  128.  
  129.     moveq    #0,d5
  130.     moveq    #0,d6
  131.  
  132.     lea    -ScratchSiz(a7),a7
  133.     move.l    a7,a3
  134.         
  135. NextPar:    move.l    #SIGBREAKF_CTRL_C,d1
  136.     sub.l    a1,a1
  137.     call    CheckBreak
  138.     tst.l    d0
  139.     bne    BreakRcvd
  140.  
  141.     move.l    (a5)+,a4
  142.  
  143. ;-------------------------------------
  144. ; Check if parameter contains | # ?
  145. ; Expects ¶meter to check in A4, A3 scratch buffer
  146.  
  147. Chk4Pat:    move.l    a4,a1
  148. ;;;    moveq    #0,d1
  149. 1$:    move.b    (a1)+,d0
  150.     beq.s    NoPattern
  151.     cmp.b    #'?',d0
  152.     beq.s    Pattern
  153.     cmp.b    #'|',d0
  154.     beq.s    Pattern
  155.     cmp.b    #'#',d0
  156.     bne.s    1$
  157.  
  158. ;-------------------------------------
  159. ; Yes, must do pattern matching
  160. ; A4 supplied file name, A3 scratch buffer
  161. ; D6 return code, D5 return value
  162.  
  163. Pattern:    lea    -ap_SIZEOF-256(a7),a7
  164.  
  165.     move.l    #SIGBREAKF_CTRL_C,ap_BreakBits(a7)
  166.     move.l    #256,ap_Length(a7)
  167.  
  168.     move.l    a4,d0
  169.     move.l    a7,a0
  170.     call    FindFirst
  171. 1$:    move.l    d0,d6
  172.     bne.s    NoFiles
  173.  
  174.     tst.l    d5    
  175.     bne.s    NoFiles
  176.  
  177.     lea    ap_SIZEOF(a7),a4
  178.     bsr.s    Remove
  179. ;;;    tst.l    d0
  180.     bne.s    2$
  181.     moveq    #RETURN_FAIL,d5
  182.  
  183. 2$:    move.l    a7,a0
  184.     call    FindNext
  185.     bra.s    1$
  186.  
  187. NoFiles:    tst.l    d6
  188.     beq.s    1$
  189.     cmp.w    #ERROR_NO_MORE_ENTRIES,d6
  190.     beq.s    1$
  191.     moveq    #RETURN_FAIL,d5
  192.  
  193. 1$:    move.l    a7,a0
  194.     call    FreeAnchorChain
  195.     lea    ap_SIZEOF+256(a7),a7
  196.  
  197.     bra.s    EndLoop
  198.  
  199. ;-------------------------------------
  200. ; No, normal remove will do
  201. ; A4 File name, A3 scratch buffer
  202. ; D5 return value
  203.  
  204. NoPattern:    bsr.s    Remove
  205. ;;;    tst.l    d0
  206.     bne.s    EndLoop
  207.  
  208.     moveq    #RETURN_FAIL,d5
  209.     move.l    #ERROR_OBJECT_NOT_FOUND,d6
  210.  
  211. EndLoop:    tst.l    d5
  212.     dbne.s    d7,NextPar
  213.  
  214.     lea    ScratchSiz+JunkSiz(a7),a7
  215.  
  216. ;-------------------------------------
  217. ; End this program
  218.  
  219. ExitOK:    moveq    #0,d2
  220.     move.l    d5,d0
  221.     beq.s    Exit
  222.     move.l    d6,d2
  223. Exit:    call    ArpExit
  224.     rts
  225.  
  226. ;-------------------------------------
  227. ; Break received -- print "***break" and exit program
  228. ; Expects pointer to break string in A1
  229.  
  230. BreakRcvd:    call    Puts
  231.     bra.s    ExitOK
  232.     move.l    #ERROR_BREAK,d2
  233. ExitFail:    moveq    #RETURN_FAIL,d0
  234.     bra.s    Exit
  235.  
  236.  
  237. RmRet:    moveq    #0,d0
  238.     rts
  239.  
  240. ;--------------------------------------------------------------------------
  241. ; Remove a file. 
  242. ; in: A4 = filename to be removed
  243. ;     A3 = 256 bytes scratch buffer area
  244. ;
  245. ; return 0 if failure, <>0 if remove succeeded
  246.  
  247. Remove:
  248.  
  249. ;-------------------------------------
  250. ; Locate the complete file name
  251. ; Expects A4 filename, A3 scratch area
  252.  
  253.     move.l    a4,d1
  254.     move.l    #ACCESS_READ,d2
  255.     call    Lock
  256.  
  257.     move.l    d0,d3
  258.     beq.s    Prnt
  259.     move.l    a3,a0
  260.     move.l    #256,d1
  261.     call    PathName
  262.  
  263.     move.l    d3,d1
  264.     call    UnLock
  265.  
  266. ;-------------------------------------
  267. ; Print information
  268. ; Expects A3 full file name
  269.  
  270. Prnt:    tst.l    d3
  271.     bne.s    1$
  272.  
  273.     move.l    a4,-(a7)
  274.     lea    RemStr(pc),a0
  275.     move.l    a7,a1
  276.     call    Printf
  277.     lea    4(a7),a7
  278.     lea    Nofile(pc),a1
  279.     call    Puts
  280.     bra.s    RmRet
  281.  
  282. 1$:    move.l    a3,-(a7)
  283.     lea    RemStr(pc),a0
  284.     move.l    a7,a1
  285.     call    Printf
  286.     lea    4(a7),a7
  287.     
  288. ;-------------------------------------
  289. ; Isolate drive name
  290. ; Expects A3 full file name
  291.  
  292. Isolate:    move.l    a3,a0
  293.  
  294. 1$:    move.b    (a0)+,d0
  295.     cmp.b    #':',d0
  296.     bne.s    1$
  297.     clr.b    (a0)
  298.  
  299. ;-------------------------------------
  300. ; Build path for rename
  301. ; Expects A2 "junk" dir name, A3 full file name, A4 original file name
  302.  
  303.     move.l    a3,a0
  304.     move.l    a2,a1
  305.     call    TackOn
  306.     move.l    a4,a0
  307.     call    BaseName
  308.     move.l    d0,a1
  309.     move.l    a3,a0
  310.     call    TackOn
  311.  
  312. ;-------------------------------------
  313. ; Munch file name so it is unique and doesn't exist
  314. ; Expects A3 full file name to move to
  315.  
  316.     moveq    #1,d4
  317. FindEnd:    move.l    a3,a0
  318. 1$:    tst.b    (a0)+
  319.     bne    1$
  320.     lea    -1(a0),a0
  321.     move.l    a0,d3
  322.     
  323. ChkEx:    move.l    a3,d1
  324.     move.l    #ACCESS_READ,d2
  325.     call    Lock
  326.     move.l    d0,d1
  327.     beq.s    1$
  328.     call    UnLock
  329.  
  330.     movem.l    a2/a3/a6,-(a7)
  331.     lea    FileFmt(pc),a0    ; Format string
  332.     move.l    d4,-(a7)
  333.     addq.l    #1,d4
  334.     move.l    a7,a1        ; Output values
  335.     move.l    d3,a3        ; Output buffer
  336.     move.l    4,a6
  337.     lea    StuffChar(pc),a2
  338.     call    RawDoFmt
  339.     lea    4(a7),a7
  340.     movem.l    (a7)+,a2/a3/a6
  341.  
  342.     bra.s    ChkEx
  343.  
  344. ;-------------------------------------
  345. ; Now rename the file
  346. ; Expects A3 new, unique junk file name, A4 original file name
  347.  
  348. 1$:    move.l    a4,d1
  349.     move.l    a3,d2
  350. Ren:    call    Rename
  351.     move.l    d0,d3
  352.     beq.s    1$
  353.     lea    Success(pc),a1
  354.     bra.s    2$
  355. 1$:    lea    Failed(pc),a1
  356. 2$:    call    Puts
  357.  
  358.     move.l    d3,d0
  359.     rts
  360.  
  361. ;-------------------------------------
  362. ; StuffChar, used for Sprintf style formatting
  363.  
  364. StuffChar:    move.b    d0,(a3)+
  365.     rts
  366.  
  367. ;-------------------------------------
  368. ;-------------------------------------
  369.  
  370. RemStr:    dc.b 'Removing %s...',0
  371. FileFmt:    dc.b ';%ld',0
  372. Success:    dc.b 'done',0
  373. Failed:    dc.b 'failed',0
  374. Nofile:    dc.b 'not found',0
  375.  
  376. Junk:    dc.b 'Junk',0
  377. CLITemp:    dc.b 'FILES/'
  378. Think:    dc.b '...',0
  379. CLIHelp:    dc.b 'Files to remove',0
  380. Usage:    dc.b 'Usage: rm <file> ...',0
  381. DosN:    dc.b 'dos.library',0
  382. ArpN:    ArpName
  383. NoArp:    dc.b 'You need arp.library V34+'
  384. NoArpEnd:
  385. TestByte:    dc.b $a
  386.  
  387.     end
  388.